﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.IO;

namespace MappitLib {
    public class Tileset {
        //***********************************************************
        #region Class Defaults
        public static int default_tile_height = 32;
        public static int default_tile_width = 32;
        public static Color default_color_bg = Color.White;
        public static int default_width = 100;
        public static int default_height = 100;
        #endregion
        //***********************************************************
        #region Error codes
        public enum Error {
            Ok,
            BitmapSizeInvalidForTileSize
        };
        #endregion
        //***********************************************************
        #region Members 
        //***********************************************************
        /*! Tile height for all tiles in this tileset. */
        private int tile_height_ = default_tile_height;
        /*! Tile width for all tiles in this tileset. */
        private int tile_width_ = default_tile_width;
        /*! Tileset bitmap. */
        private Bitmap bitmap_ = null;
        /*! Tileset background color. */
        private Color color_bg_ = default_color_bg;
        /*! Bitmap filename.
         */
        private string pathname_;
        #endregion
        //***********************************************************
        #region Properties
        public Bitmap Bitmap {
            get {
                return bitmap_;
            }
        }
        /*! Tileset bitmap width (in tiles).
         */
        public int Width {
            get {
                if (bitmap_ != null)
                    return Bitmap.Size.Width / tile_width_;
                else
                    return 0;
            }
        }
        /*! Tileset bitmap height (in tiles).
         */
        public int Height {
            get {
                if (bitmap_ != null)
                    return Bitmap.Size.Height / tile_height_;
                else
                    return 0;
            }
        }
        /*! Tile width.
         */
        public int TileWidth {
            get {
                return tile_width_;
            }
        }
        /*! Tile height.
         */
        public int TileHeight {
            get {
                return tile_height_;
            }
        }
        /*! Bitmap filename.
         */
        public string Pathname {
            get { return pathname_; }
        }
        /*! Filename.
         */
        public string Filename {
            get { return Path.GetFileName(Pathname); }
        }
        #endregion
        //***********************************************************
        #region Methods
        /*! Construction.
         */
        public Tileset() {
        }
        public Tileset(string filename) {
            LoadBitmap(filename);
        }
        /*! Load a bitmap file.
         */
        public Error LoadBitmap(string filename) {
            Bitmap new_bitmap = new Bitmap(filename);
            Error result = Error.Ok;
            result = ValidateBitmap(new_bitmap);
            if (result != Error.Ok)
                return result;
            bitmap_ = new_bitmap;
            pathname_ = filename;
            return Error.Ok;
        }
        /*! Check properties of bitmap to make sure it's a valid tileset.
         */
        public Error ValidateBitmap(Bitmap bitmap) {
            // bitmap height and width must be factors of tile height and width
            if ((bitmap.Size.Height % tile_height_ != 0) ||
                bitmap.Size.Width % tile_width_ != 0) {
                    return Error.BitmapSizeInvalidForTileSize;
            }
            // everything checks out!
            return Error.Ok;
        }
        /*! Get a tile coordinate from a pixel coordinate.
         */
        public Point GetTilePos(Point pixel_pos) {
            if (bitmap_ != null) {
                return new Point(
                    pixel_pos.X / this.TileWidth,
                    pixel_pos.Y / this.TileHeight);
            } else {
                return new Point();
            }
        }
        /*! Get the rectangular region of the bitmap that corresponds to a tile coord.
         */
        public Rectangle GetTileRect(Point tile_position) {
            if (bitmap_ != null) {
                return new Rectangle(
                    tile_position.X * this.TileWidth,
                    tile_position.Y * this.TileHeight,
                    this.TileWidth,
                    this.TileHeight);
            } else {
                return new Rectangle();
            }
        }
        #endregion
        //***********************************************************
    }
}
